package com.koliko.from_now_on.servlet;

import com.koliko.from_now_on.bean.Team;
import com.koliko.from_now_on.service.ITeamService;
import com.koliko.from_now_on.service.impl.TeamServiceImpl;
import com.koliko.from_now_on.utils.Constant;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.swing.*;
import java.io.IOException;
import java.util.List;

@WebServlet(name="TeamServlet",urlPatterns = {"/teamServlet"})
public class ITeamServlet extends HttpServlet {
    private ITeamService teamService = new TeamServiceImpl();


    /**
     * 处理doget
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     */
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doPost(req, resp);
    }

    /**
     * 处理dopost
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     */
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("UTF-8");
        String type = req.getParameter(Constant.REQUEST_PARAMETER_TYPE);
        if (type != null && !"".equals(type)) {
            if (Constant.SERVLET_TYPE_SAVE.equals(type)) {
                //添加用户信息
                saveTeam(req, resp);
            } else if (Constant.SERVLET_TYPE_QUERYGUOJI.equals(type)) {
                //查询国际信息
                queryGJ(req,resp);
            } else if (Constant.SERVLET_TYPE_QUERYWUSHAN.equals(type)) {
                //查询五山信息
                queryWS(req,resp);
            } else if (Constant.SERVLET_TYPE_QUERYDAXUECHENG.equals(type)) {
                //查询大学城信息
                queryDXC(req,resp);
            } else if (Constant.SERVLET_TYPE_QUERYALL.equals(type)) {
                //查询所有信息
                queryTeam(req, resp);
            } else if (Constant.SERVLET_TYPE_CHECKSAVE.equals(type)) {
                //检查报名状态并且保存选题
                String teamTitle= req.getParameter("team_title");
                String stu1Id=req.getParameter("stu1_id");
                String teamChoice=req.getParameter("team_choice");
                Team team = teamService.CheckTeam(stu1Id);

                if(team != null) {
                    //TODO 写入插入操作
                    team.setTeamTitle(teamTitle);
                    team.setTeamChoice(teamChoice);
                    Integer count = teamService.addTitle(team);
                    if(count>0){
                        //插入成功 跳转链接
                        resp.sendRedirect("/Sub_SecondOk.html");
                    }else {
                        resp.sendRedirect("/Sub_SecondFail.html");
                    }
                } else{
                    //没有检测到信息
                    resp.sendRedirect("/CheckNull.html");
                }

            } else if (Constant.SERVLET_TYPE_CHECK.equals(type)) {
                //检查报名状态
                String stu1Id=req.getParameter("stu1_id");
                Team team = teamService.CheckTeam(stu1Id);
                if(team != null) {
                    List<Team> list = this.teamService.getTeam((Team)null);
                    req.setAttribute("team",team);
                    //处理跳转链接
                    req.getRequestDispatcher("/Check.jsp").forward(req, resp);
                } else{
                    //没有检测到信息
                    resp.sendRedirect("/CheckNull.html");
                }

            }
        }else {
            //查询
            queryTeam(req, resp);
        }



    }

    private void queryTeam(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        List<Team> list = this.teamService.getTeam((Team)null);
        req.setAttribute("list",list);
        //处理跳转链接
        req.getRequestDispatcher("/form.jsp").forward(req, resp);
    }
    private void queryDXC(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        List<Team> list = this.teamService.queryDXC((Team)null);
        req.setAttribute("list",list);
        //处理跳转链接
        req.getRequestDispatcher("/form.jsp").forward(req, resp);
    }
    private void queryWS(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        List<Team> list = this.teamService.queryWS((Team)null);
        req.setAttribute("list",list);
        //处理跳转链接
        req.getRequestDispatcher("/form.jsp").forward(req, resp);
    }
    private void queryGJ(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        List<Team> list = this.teamService.queryGJ((Team)null);
        req.setAttribute("list",list);
        //处理跳转链接
        req.getRequestDispatcher("/form.jsp").forward(req, resp);
    }

    private void saveTeam(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        Team team = new Team();
        team.setTeamName(req.getParameter("team_name"));
        team.setTeamArea(req.getParameter("team_area"));
        team.setStu1Phone(req.getParameter("stu1_phone"));
        team.setStu1Id(req.getParameter("stu1_id"));
        team.setStu2Id(req.getParameter("stu2_id"));
        team.setStu3Id(req.getParameter("stu3_id"));
        team.setStu1Name(req.getParameter("stu1_name"));
        team.setStu2Name(req.getParameter("stu2_name"));
        team.setStu3Name(req.getParameter("stu3_name"));
        team.setStu1Class(req.getParameter("stu1_class"));
        team.setStu2Class(req.getParameter("stu2_class"));
        team.setStu3Class(req.getParameter("stu3_class"));

        Integer count = teamService.addTeam(team);
        if(count>0){
            //插入成功 跳转链接
            resp.sendRedirect("/Sub_FirstOk.html");
        }else {
            resp.sendRedirect("/Sub_FirstFail.html");
        }
    }

}
